package cz.khfotbal.beans;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.khfotbal.dao.UserDao;
import cz.khfotbal.domain.Team;
import cz.khfotbal.domain.User;
import cz.khfotbal.service.TeamService;
import cz.khfotbal.service.UserService;

@Component
@Scope(value = "session")
public class TeamBean {

	@Autowired
	private TeamService teamService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserDao userDao;

	private Team newTeam;
	private Team selectedTeam = new Team();
	private List<Team> filteredTeams;
	private List<String> usernameList = new ArrayList<String>();
	private String selectedUSername;

	public TeamBean() {
		newTeam = new Team();
	}

	public void setupUsername() {
		selectedUSername = selectedTeam.getUser().getUsername();
	}

	public List<Team> getTeamsList() {
		return teamService.getAllTeams();
	}

	public void updateTeam() {
		if (selectedTeam != null) {
			if (!selectedUSername.isEmpty()) {
				User u = userDao.findUser(selectedUSername);
				selectedTeam.setUser(u);
			}
			teamService.updateTeam(selectedTeam);
		}
		selectedTeam = new Team();
	}

	public void createTeam() {
		teamService.createTeam(newTeam);
		newTeam = new Team();
	}

	public void deleteTeam() {
		teamService.deleteTeam(selectedTeam);
		selectedTeam = new Team();
	}

	public Team getNewTeam() {
		return newTeam;
	}

	public void setNewTeam(Team newTeam) {
		this.newTeam = newTeam;
	}

	public Team getSelectedTeam() {
		return selectedTeam;
	}

	public void setSelectedTeam(Team selectedTeam) {
		this.selectedTeam = selectedTeam;
	}

	public List<Team> getFilteredTeams() {
		return filteredTeams;
	}

	public void setFilteredTeams(List<Team> filteredTeams) {
		this.filteredTeams = filteredTeams;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public List<String> getUsernameList() {
		List<User> list = userService.getAllUsers();
		usernameList = new ArrayList<String>();
		for (User u : list) {
			usernameList.add(u.getUsername());
		}
		return usernameList;
	}

	public void setUsernameList(List<String> usernameList) {
		this.usernameList = usernameList;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public String getSelectedUSername() {
		return selectedUSername;
	}

	public void setSelectedUSername(String selectedUSername) {
		this.selectedUSername = selectedUSername;
	}

}
